// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/TabbedMenuWidget.h"
#include "UserWidget/SingleTabButtonWidget.h"
#include "Components/HorizontalBox.h"

void UTabbedMenuWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	SelectedIndex = 1;
	Widget_SingleTabButton = (USingleTabButtonWidget*)GetWidgetFromName(TEXT("Widget_SingleTabButton"));
	Widget_SingleTabButton_C_3 = (USingleTabButtonWidget*)GetWidgetFromName(TEXT("Widget_SingleTabButton_C_3"));
	Widget_SingleTabButton_C_4 = (USingleTabButtonWidget*)GetWidgetFromName(TEXT("Widget_SingleTabButton_C_4"));
	Widget_SingleTabButton_C_5 = (USingleTabButtonWidget*)GetWidgetFromName(TEXT("Widget_SingleTabButton_C_5"));
}

void UTabbedMenuWidget::SwitchTabs(EMenuNavigation LNavigation) {
	UpdateSelectedIndex(LNavigation);
	TArray<UWidget*> Temp = ButtonsList->GetAllChildren();
	for (int32 i = 0; i < Temp.Num(); ++i) {
		USingleTabButtonWidget* temp = Cast<USingleTabButtonWidget>(Temp[i]);
		if (temp) temp->SetUnselected();
	}
	USingleTabButtonWidget* temp = Cast<USingleTabButtonWidget>(Temp[SelectedIndex]);
	if (temp) temp->SetSelected();
	OnNavigate.Broadcast(SelectedIndex);
}

void UTabbedMenuWidget::UpdateSelectedIndex(EMenuNavigation LNavigation) {
	switch (LNavigation)
	{
	case EMenuNavigation::Select:
		break;
	case EMenuNavigation::Previous:
		SelectedIndex = SelectedIndex - 1 < 0 ? ButtonsList->GetChildrenCount() - 1 : SelectedIndex - 1;
		break;
	case EMenuNavigation::Next:
		SelectedIndex = SelectedIndex + 1 == ButtonsList->GetChildrenCount() ? 0 : SelectedIndex + 1;
		break;
	default:
		break;
	}
}